/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package extension.room.analyse;

import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import log.Debug;

/**
 *
 * @author daz
 */
public class CardDealer {

    private int[] cardSetId;
    private List<Integer> cardList;
    private Random random = new Random();
    private int numberCards = 52;

    public CardDealer() {
        cardList = new LinkedList<Integer>();
        cardSetId = new int[numberCards];
        for (int i = 0; i < numberCards; i++) {
            cardSetId[i] = i;
        }
    }

    public void shuffleCard() {
        cardList.clear();
        for (int i = 0; i < cardSetId.length; i++) {
            int r = random.nextInt(100);
            int temp = cardSetId[i];
            cardSetId[i] = cardSetId[r % numberCards];
            cardSetId[r % numberCards] = temp;
        }
        if (Debug.isDebug) {
//            cardSetId = DataTest.getCardSet29();
        }
        for (int i = 0; i < cardSetId.length; i++) {
            cardList.add(cardSetId[i]);
        }

        Debug.d("card set :" + cardList.toString());
    }

    public int nextCard() {
        if (cardList.isEmpty()) {
            return -1;
        }
        return cardList.remove(0);
    }

    public int getSize() {
        return cardList.size();
    }

    public static void main(String[] args) {
        CardDealer dealer = new CardDealer();
        dealer.shuffleCard();
        int numberCards = 52;
//        int[] cardIds = new int[numberCards];
        int[] cardIds = DataTest.getCardSet28();
        for (int i = 0; i < numberCards; i++) {
            cardIds[i] = dealer.nextCard();
        }
        printCard(cardIds);

        //check duplicate
//        for (int i = 0; i < numberCards; i++) {
//            for (int j = 0; j < numberCards; j++) {
//                if (i != j && cardIds[i] == cardIds[j]) {
//                    Debug.d("duplicate :" + i + "," + j + "-" + cardIds[i]);
//                    break;
//                }
//            }
//        }

    }

    private static void printCard(int[] listCards) {
        for (int i = 0; i < listCards.length; i++) {
            if (i % 13 == 0 && i != 0) {
                Debug.d("");
            }
            Debug.n(Card.getRank(listCards[i]) + Card.getSuit(listCards[i]));
        }

        System.out.println();
    }

    private int getCardRank(int cardId) {
        return (cardId - 2 + 13) % 13;
    }

    public void addCard(int cardId) {
        cardList.add(cardId);
    }
}
